package edu.bbu.bibliospring.api.cdiassembler;

import edu.bbu.bibliospring.api.exception.BibliospringApiException;
import edu.bbu.bibliospring.api.resources.TestResource;
import edu.bbu.bibliospring.backend.model.Author;
import edu.bbu.bibliospring.common.dto.AuthorDto;
import org.apache.log4j.Logger;

import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * Created by erika on 6/13/14.
 * Assembler class transforming the model and dto of the Author entity
 */

@Stateless
public class AuthorAssembler implements Assembler<Author, AuthorDto> {

    private static final Logger LOG = Logger.getLogger(TestResource.class);

    @Override
    public Author createModel() {
        LOG.info("AuthorAssembler - new Author created");
        return new Author();
    }

    @Override
    public AuthorDto createDto() {
        LOG.info("AuthorAssembler - new AuthorDto created");
        return new AuthorDto(Author.class.getName());
    }

    @Override
    public AuthorDto modelToDto(Author model) {
        LOG.info("AuthorAssembler - modelToDto");
        AuthorDto authorDto = createDto();
        authorDto.setId(model.getId());
        authorDto.setFirstName(model.getFirstName());
        authorDto.setLastName(model.getLastName());
        return authorDto;
    }

    @Override
    public Author dtoToModel(final AuthorDto dto) throws BibliospringApiException {
        LOG.info("AuthorAssembler - dtoToModel");
        Author author = createModel();
        author.setId(dto.getId());
        author.setFirstName(dto.getFirstName());
        author.setLastName(dto.getLastName());
        return author;
	}
}

